<?
/*
 * Created on Nov 6, 2011
 *
 * Based on: http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html
 * 
 */
require_once("bizlayer/player_mgmt.php");
session_start();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
	<head>
		<title>Walter J Corbett Intivational</title>
		<link href="assets/styles/liquid.css" rel="stylesheet" type="text/css" />
		<link href="assets/styles/wjci.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript" src="assets/js/jquery-1.6.2.min.js"></script>
		<script type="text/javascript" src="assets/js/jquery.date-format.js"></script>
		<script type="text/javascript" src="assets/js/jquery.form.js"></script>
		<script type="text/javascript">
			var nPlayerId;
			var oPlayer;
			$(document).ready(function(){
				
				nPlayerId = parseInt($("#playerid").val());
				
				var soapUrl = "soap/GetSinglePlayer.php?playerid=" + nPlayerId;
				$.ajax({
					url:soapUrl,
					dataType:"json",
					contentType:"application/json",
					success:function(data,textStatus,jqXHR){
						oPlayer = data;
						initializeTowns(oPlayer);
					},
					error:function(jqXHR, textStatus, errorThrown){
						
						var aMsg = new Array();
						aMsg.push("Shit went wrong.");
						aMsg.push(textStatus);
						aMsg.push(errorThrown);
						alert(aMsg.join("\n"));
					}
				});
			});
			
			function setDates(){
				var oMonth = parseInt($("#dobMonth").val());
				var oYear = parseInt($("#dobYear").val());
				var aShortM = [2,4,6,9,11];
				var maxDay = 31;
				
				if(oMonth == 2){
					maxDay = ((oYear % 4) > 0)?28:29;
				}
				else if(jQuery.inArray(oMonth,aShortM) > -1){
					maxDay = 30;
				}
				
				$("#dobDate >option").remove();
				
				var oOptDates = $("#dobDate");
				for(var i=1;i<=maxDay;i++){
					oOptDates.append($("<option/>").val(i).text(i));
				}
			}
			
			function initializeTowns(vPlayer){
				$("#pBio").html(vPlayer.bio);
				$("#taBio").val(vPlayer.bio);
				$("#hometown").text(vPlayer.homeTown);
				$("#currentcity").text(vPlayer.currentCity);
				$("#txtBxNativeTown").val(vPlayer.homeTown);
				$("#txtBxCurrentCity").val(vPlayer.currentCity);
				
				
				var _dob = oPlayer.dob.split("/");
				var _y = parseInt(_dob[0]);
				var _m = parseInt(_dob[1]);
				var _d = parseInt(_dob[2]);
				
				if(oPlayer.dob != null && oPlayer.dob != ""){
					$("#dob").text($.format.date(new Date(oPlayer.dob),"MMM dd yyyy"));	
				}
				$("#dobMonth").val(_m);
				$("#dobYear").val(_y);
				$("#dobYear").change();
				$("#dobDate").val(_d);
			}
			
			function save(){
				
				oPlayer.currentCity = $("#txtBxCurrentCity").val();
				oPlayer.homeTown = $("#txtBxNativeTown").val();
				oPlayer.dob = $("#dobYear").val()+"/"+$("#dobMonth").val()+"/"+$("#dobDate").val();
				oPlayer.bio = $("#taBio").val();
				
				
				
				$.post("soap/SaveSinglePlayer.php", oPlayer ,function(data,textStatus){
					toggleState(false);
					initializeTowns(data);
				}).error(function(jqXHR, textStatus, errorThrown){
					alert("Error\n" + textStatus + "\n" + errorThrown);
				});	
			}
			
			function toggleState(isEdit){
				if(isEdit){
					$("#towns_display").hide();
					$("#towns_edit").show();
					$("#lnkEdit").hide();
					$("#lnkSave").show();	
					$("#pBio").hide();
					$("#bio_edit").show();
				}
				else{
					$("#towns_display").show();
					$("#towns_edit").hide();
					$("#lnkEdit").show();
					$("#lnkSave").hide();
					$("#pBio").show();
					$("#bio_edit").hide();	
				}
				
			}
		</script>
	</head>
<body>
<div id="maincontainer">
	
	<?php
		require_once("components/header.inc.php");
		
		if(!isset($_GET["playerid"]))
			header("Location:index.php");
		
		if(!isset($me) && isset($_SESSION["me"]))
			$me = $_SESSION["me"];
		
		
		
		$playerid = $_GET["playerid"];
		$playerid = (is_numeric($playerid))?$playerid:-1;
		
		$isEditable = (isset($me) && $me->playerid == $playerid);
		
		$player = PlayerMgmt::getPlayerById($playerid);
		
		printf("<input type=\"hidden\" id=\"playerid\" name=\"playerid\" value=\"%s\">",$playerid);
		
		$imageData = PlayerMgmt::getProfilePic($playerid);
		$noImage = (count($imageData)==0 || $imageData["photo_id"] < 1);
		
	?>
	<form id="profileForm" method="post" action="profile.php" enctype="multipart/form-data" >
	<div id="contentwrapper">
		<div id="contentcolumn">
			<div class="innertube">
				<h3 style="margin-top:5px;float:left;width:70%"><? printf("%s %s",$player->firstName,$player->lastName); ?></h3>
				<div style="width:29%;float:right;margin-top:5px;text-align:right;">
					<?if($isEditable){?>
						<a id="lnkEdit" href="#" onclick="toggleState(true)">Edit</a>
						<a id="lnkSave" href="#" onclick="save()" style="display:none;">Save</a>
					<?}?>
				</div>
				<div id="blankimage" style="width:70%;float:left;text-align:center;">
					<div style="width:100%;text-align:right">
						<?if($isEditable){?>
							<a href="manage_photos.php" style="margin-right:5px;">Manage Photos</a>
						<?}?>
					</div>
					<?if($noImage){?>
						<div style="margin-top:100px;text-align:center;">Image not available.</div>
					<?}
					else{
						printf("<img src=\"components/display_photo.php?photo_id=%d&size=m\" alt=\"%s\"/>",$imageData["photo_id"],$imageData["caption"]);	
					}?>
				</div>
				<div style="width:29%;float:left;padding-left:5px;">
					<div id="towns_display">
						<ul class="nopadding">
							<li>DOB:<br/><span id="dob"></li>
							<li>From:<br/><span id="hometown"></span></li>
							<li>Lives:<br/><span id="currentcity"></span></li>
						</ul>
						
					</div>
					<div id="towns_edit" style="display:none;">
						<ul class="nopadding">
							<li>
								DOB:<br/>
								<select id="dobMonth" name="dobMonth" onchange="setDates();">
									<?
									for($i=1;$i<13;$i++){
										printf("<option value=\"%s\">%s</option>",$i,strftime("%b",strtotime("2011/".$i."/01")));
									}
									?>
								</select>
								<select id="dobDate" name="dobDate"></select>
								<select id="dobYear" name="dobYear" onchange="setDates();">
									<?
									for($i=1950;$i<=2030;$i++){
										printf("<option value=\"%s\">%s</option>",$i,$i);
									}
									?>
								</select>
							</li>
							<li>From:<br/><input type="text" id="txtBxNativeTown" name="txtBxNativeTown"/></li>
							<li>Lives:<br/><input type="text" id="txtBxCurrentCity" name="txtBxCurrentCity"/></li>
						</ul>
						
					</div>
				</div>
				<br style="clear:both"/>
				<div style="clear:both;width:100%;margin-top:10px;">
					<strong>Bio</strong>
					<div id="pBio"></div>
					<div id="bio_edit" style="display:none;">
						<textarea id="taBio" name="taBio" rows="6" cols="70"></textarea>
					</div>
				</div>
				<hr/>
			</div>
		</div>
	</div>
	
	<div id="leftcolumn">
		<div class="innertube" align="left">
			<?include_once("components/side_menu.php");?>
		</div>
	</div>
	<?php
	require_once("components/footer.inc.php");
	?>
	</form>
</div> 
</body>
</html>
